package gestion.camping.vue;

import gestion.camping.controleur.PersonneController;
import gestion.camping.dto.Personne;
import gestion.camping.utils.MessagesUtil;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

public class ViewPersonneListe extends JPanel implements ActionListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = -1923064145354238975L;
	
	private TablePersonne table;

	private List<Personne> listPersonne;
	private PersonneController controller;
	private JButton btnRechercher;
	private JTextField textNom;
	private JLabel lblNom;
	private JLabel lblPrenom;
	private JTextField textPrenom;
	private JButton btnValider;
	private JButton btnAjouter;

	/**
	 * Create the panel.
	 */
	public ViewPersonneListe(PersonneController controller) {
	
		this.controller = controller;
		
		this.listPersonne = controller.getListPersonne();
		
		initCompoment();
	}
	
	public void initCompoment(){
	
		setBounds(0, 45, 1150, 700);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.15, 0.2, 0.15, 0.2, 0.15, 0.15};
		gridBagLayout.rowWeights = new double[]{0.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.1};
		setLayout(gridBagLayout);
		
		btnRechercher = new JButton(MessagesUtil.formatMessage("personne.liste.btn.rechercher"));
		btnRechercher.addActionListener(this);
		
		lblNom = new JLabel(MessagesUtil.formatMessage("personne.liste.critere.nom"));
		GridBagConstraints gbc_lblNom = new GridBagConstraints();
		gbc_lblNom.insets = new Insets(0, 0, 5, 5);
		gbc_lblNom.anchor = GridBagConstraints.EAST;
		gbc_lblNom.gridx = 1;
		gbc_lblNom.gridy = 2;
		add(lblNom, gbc_lblNom);
		
		textNom = new JTextField(controller.getNom());
		GridBagConstraints gbc_textNom = new GridBagConstraints();
		gbc_textNom.insets = new Insets(0, 0, 5, 5);
		gbc_textNom.fill = GridBagConstraints.HORIZONTAL;
		gbc_textNom.gridx = 2;
		gbc_textNom.gridy = 2;
		add(textNom, gbc_textNom);
		textNom.setColumns(10);
		
		lblPrenom = new JLabel(MessagesUtil.formatMessage("personne.liste.critere.prenom"));
		GridBagConstraints gbc_lblPrenom = new GridBagConstraints();
		gbc_lblPrenom.anchor = GridBagConstraints.EAST;
		gbc_lblPrenom.insets = new Insets(0, 0, 5, 5);
		gbc_lblPrenom.gridx = 3;
		gbc_lblPrenom.gridy = 2;
		add(lblPrenom, gbc_lblPrenom);
		
		textPrenom = new JTextField(controller.getPrenom());
		GridBagConstraints gbc_textPrenom = new GridBagConstraints();
		gbc_textPrenom.fill = GridBagConstraints.HORIZONTAL;
		gbc_textPrenom.insets = new Insets(0, 0, 5, 5);
		gbc_textPrenom.gridx = 4;
		gbc_textPrenom.gridy = 2;
		add(textPrenom, gbc_textPrenom);
		textPrenom.setColumns(10);
		GridBagConstraints gbc_btnRechercher = new GridBagConstraints();
		gbc_btnRechercher.anchor = GridBagConstraints.EAST;
		gbc_btnRechercher.insets = new Insets(0, 0, 5, 5);
		gbc_btnRechercher.gridx = 4;
		gbc_btnRechercher.gridy = 4;
		add(btnRechercher, gbc_btnRechercher);
		
		table = new TablePersonne();
		GridBagConstraints gbc_table = new GridBagConstraints();
		gbc_table.gridwidth = 4;
		gbc_table.insets = new Insets(0, 0, 5, 5);
		gbc_table.fill = GridBagConstraints.BOTH;
		gbc_table.gridx = 1;
		gbc_table.gridy = 5;
		JScrollPane scrollPane = new JScrollPane(table);
		add(scrollPane, gbc_table);
		
		btnAjouter = new JButton(MessagesUtil.formatMessage("personne.ajout.title"));
		btnAjouter.addActionListener(this);
		GridBagConstraints gbc_btnAjouter = new GridBagConstraints();
		gbc_btnAjouter.anchor = GridBagConstraints.EAST;
		gbc_btnAjouter.insets = new Insets(0, 0, 0, 5);
		gbc_btnAjouter.gridx = 3;
		gbc_btnAjouter.gridy = 6;
		add(btnAjouter, gbc_btnAjouter);
		
		btnValider = new JButton(MessagesUtil.formatMessage("personne.liste.btn.modifier"));
		btnValider.addActionListener(this);
		GridBagConstraints gbc_btnValider = new GridBagConstraints();
		gbc_btnValider.anchor = GridBagConstraints.WEST;
		gbc_btnValider.insets = new Insets(0, 0, 0, 5);
		gbc_btnValider.gridx = 4;
		gbc_btnValider.gridy = 6;
		add(btnValider, gbc_btnValider);
		
	}

	public List<Personne> getListPersonne() {
		return listPersonne;
	}

	public void setListPersonne(List<Personne> listPersonne) {
		this.listPersonne = listPersonne;
		
		update();
	}
	
	private void update(){
		int indexElementSelected = table.getSelectedRow();
		
		table.setModel(new TablePersonneModel(listPersonne, table.getColumnNames()));
		table.setShowGrid(true);
		table.setVisible(true);
		
		this.repaint();
		
		if(indexElementSelected >= 0){
			table.setRowSelectionInterval(indexElementSelected, indexElementSelected);
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnRechercher){
			this.controller.rechercher(textNom.getText().toUpperCase(), textPrenom.getText().toUpperCase());
		}else if(e.getSource() == btnValider){
			Personne selectedPersonne = listPersonne.get(this.table.getSelectedRow());			
			
			this.controller.modifier(selectedPersonne);
		}else if(e.getSource() == btnAjouter){
			this.controller.ajouter();
		}
		
	}
	
	public Personne getSelectedPersonne(){	
		return table.getSelectedRow() < 0 ? null : listPersonne.get(table.getSelectedRow());
	}

}
